/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinarow.main;

import com.athena.objects.Game;
import com.athena.objects.Player;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import com.smartfoxserver.v2.extensions.SFSExtension;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
import sfs2x.extension.fourinarow.utils.Commands;
import sfs2x.extension.fourinarow.utils.Data;

/**
 *
 * @author dodinhchien
 */
public class FourinarowExtension extends SFSExtension {
    
    private ConcurrentHashMap<Integer, Game> gamesList = null;
    private ConcurrentHashMap<Integer, Player> usersList = null;

    public ConcurrentHashMap<Integer, Player> getUsersList() {
        return usersList;
    }
    
    private SFSArray arrayMC;

    public SFSArray getArrayMC() {
        return arrayMC;
    }
    public void setArrayMC(SFSArray arrayMC) {
        this.arrayMC = arrayMC;
    }
    


    public ConcurrentHashMap<Integer, Game> getGamesList() {
        return gamesList;
    }
    
    @Override
    public void init() {
        //taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(new TastRunner(), 0, 1, TimeUnit.SECONDS);
        gamesList = new ConcurrentHashMap<>();
        usersList = new ConcurrentHashMap<>();
        try {
            Data.load(this);
        } catch (SQLException ex) {
            trace(ExtensionLogLevel.ERROR, "cant get data from db", ex.toString());
        }
        // Hnadle Event : user login
        addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandle.class);
        
        // Handle Event : User logout
        addEventHandler(SFSEventType.USER_LOGOUT, LogoutEventHandle.class);
        
        addEventHandler(SFSEventType.USER_LEAVE_ROOM, LeaveRoomEventHandle.class);
        // Handle Event : User disconnect
        addEventHandler(SFSEventType.USER_DISCONNECT, DisconnectEventHandle.class);

        addRequestHandler(Commands.CREATE_GAME, CreateGameRequest.class);
        
        addEventHandler(SFSEventType.USER_JOIN_ZONE, UserJoinZoneHandle.class);
        
        addEventHandler(SFSEventType.USER_JOIN_ROOM, UserjoinRoomHandle.class);
        
        //addEventHandler(SFSEventType.ROOM_ADDED, RoomADDEventHandle.class);
       
        addEventHandler(SFSEventType.ROOM_REMOVED, RoomRemoveHandle.class);
        
        addRequestHandler(Commands.START, StartHandle.class);
        
        addRequestHandler(Commands.MOVE, MoveHandle.class);
        
        addRequestHandler(Commands.LEAVE, LeaveHandle.class);
        
        
    }

    

    @Override
    public void destroy() {

        //remove event when this extension stop
        removeEventHandler(SFSEventType.USER_LOGIN);
        removeEventHandler(SFSEventType.USER_LOGOUT);
        removeEventHandler(SFSEventType.USER_DISCONNECT);
        removeEventHandler(SFSEventType.USER_LEAVE_ROOM);
        removeRequestHandler(Commands.CREATE_GAME);
        removeEventHandler(SFSEventType.USER_JOIN_ZONE);
        removeEventHandler(SFSEventType.USER_JOIN_ROOM);
        removeEventHandler(SFSEventType.ROOM_ADDED);
        removeEventHandler(SFSEventType.ROOM_REMOVED);
        removeRequestHandler(Commands.START);
        removeRequestHandler(Commands.MOVE);
    }
}
